package at.fhj.itm10.ticketmaster.helper;
/**
 * 
 * @author Gerald Hambrosch
 *
 */
import java.sql.SQLException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

import at.fhj.itm10.ticketmaster.beans.AddressBean;
import at.fhj.itm10.ticketmaster.beans.EventBean;
import at.fhj.itm10.ticketmaster.dao.DAOException;
import at.fhj.itm10.ticketmaster.models.Address;

public class AddressConverter implements Converter {

	private Address a;
	private AddressBean ab;
	private EventBean b;

	public Object getAsObject(FacesContext context, UIComponent component, String value)
			throws ConverterException {
		
		if (value == null || value.equals("")) {
            return "";
        } 
		else 
		{
				b = new EventBean();
				ab = new AddressBean();
                long id = Long.parseLong((value));
                	
							try {
								a = ab.getDao().findById(id);
							} catch (DAOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}						
							try {

								b.setAddress(a);
								b.doExtendedSearch();
							} catch (DAOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							} catch (SQLException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						
					
                	System.out.println("call"+a.getName());
                    return a;
        }

	}

	public String getAsString(FacesContext context, UIComponent component, Object value)
			throws ConverterException {
	       if (value == null || value.equals("")) {
	            return "";
	        } else {
	            return String.valueOf(((Address) value).getId());
	        }

	}

}
